package cn.sangyy.gift.controller;

import cn.sangyy.gift.dto.TenantRegisterDto;
import cn.sangyy.gift.exception.BusinessException;
import cn.sangyy.gift.service.ITenantService;
import cn.sangyy.gift.domain.Tenant;
import cn.sangyy.gift.query.TenantQuery;
import cn.sangyy.gift.common.util.AjaxResult;
import cn.sangyy.gift.common.util.PageList;
import cn.sangyy.gift.utils.CosUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/tenant")
public class TenantController {
    @Autowired
    public ITenantService tenantService;

    /**
     * 保存和修改公用的
     * @param tenant  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Tenant tenant){
        try {
            if( tenant.getId()!=null)
                tenantService.updateById(tenant);
            else
                tenantService.insert(tenant);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            tenantService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    /**
    * 通过id获取对象信息
    * @param id
    * @return
    */
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id) {
        try {
            Tenant tenant = tenantService.getById(id);
            return AjaxResult.me().setResultObj(tenant);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage("获取一个失败！"+e.getMessage());
        }
    }

    /**
    * 查看所有
    * @return
    */
    @GetMapping("/list")
    public AjaxResult list(){
        try {
            List<Tenant> list = tenantService.selectList(null);
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage("获取所有失败！"+e.getMessage());
        }
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/listPage")
    public AjaxResult listPageByQuery(@RequestBody TenantQuery query) {
        PageList<Tenant> pageList = tenantService.selectPageList(query);
        return AjaxResult.me().setResultObj(pageList);
    }

    /**
    * @author Sangyy
    * 店铺入驻
    */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody @Valid TenantRegisterDto tenantRegisterDto){
        return tenantService.settlement(tenantRegisterDto);
    }
    /**
    * @author Sangyy
    * 文件上传到cos
    */
    @PostMapping("/uploadByCos")
    public AjaxResult uploadImageByCos(@RequestBody MultipartFile file){
        String path = null;
        try {
            path = CosUtils.uploadImage(file);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("文件上传失败！");
        }
        return AjaxResult.me().setResultObj(path);
    }

    /**
    * @author Sangyy
    * 删除文件
    */
    @DeleteMapping("/deleteByCos")
    public AjaxResult deleteByCos(@RequestParam("path") String path){
        CosUtils.deleteImage(path);
        return AjaxResult.me();
    }

    /**
     * @author Sangyy
     * 店铺激活
     */
    @GetMapping("/active/{id}")
    public String active(@PathVariable Long id){
        String msg = "激活成功！";
        Tenant tenant = tenantService.selectById(id);
        if(tenant.getState() == 3){
            msg = "已激活，请勿重复激活";
        }
        tenant.setState(3);
        tenantService.updateById(tenant);
        return msg;
    }

}
